import pandas as pd
import os
import re

# 文件夹路径
input_folder = r'c:\Users\admin\Desktop\homework\课程信息'

# 存储所有课程信息，包括来源文件和位置
all_course_info = []

# 遍历所有Excel文件
for filename in os.listdir(input_folder):
    if filename.endswith('.xls'):
        file_path = os.path.join(input_folder, filename)
        print(f'\n=== 处理文件: {filename} ===')
        
        # 读取Excel文件
        df = pd.read_excel(file_path)
        
        # 遍历每个单元格
        for index, row in df.iterrows():
            for col in df.columns:
                cell_value = row[col]
                if pd.notna(cell_value):
                    cell_str = str(cell_value)
                    
                    # 查找所有可能的课程信息格式
                    # 格式1: 课程名称/(节次)周次/ 地点/教师/人数
                    pattern1 = r'([^/]+)/(?:\([^)]+\))?([^/]+)/\s*([^/]+)/([^/]+)/\d+'
                    matches1 = re.finditer(pattern1, cell_str)
                    for match in matches1:
                        course_name = match.group(1).strip()
                        all_course_info.append((filename, index+1, col, course_name))
                    
                    # 格式2: 实践课程：美育★李红(共1周)/1周;
                    pattern2 = r'(?:实践课程|其他课程)：([^★☆]+)[★☆]([^(]+)'
                    matches2 = re.finditer(pattern2, cell_str)
                    for match in matches2:
                        course_name = match.group(1).strip()
                        all_course_info.append((filename, index+1, col, course_name))

# 显示所有找到的课程信息
print(f'\n=== 找到的所有课程信息 ({len(all_course_info)}条) ===')
for info in all_course_info:
    print(f'{info[0]} - 行{info[1]}, 列{info[2]}: {info[3]}')

# 去重后的课程列表
unique_courses = list(set([info[3] for info in all_course_info]))
print(f'\n=== 去重后的课程列表 ({len(unique_courses)}门) ===')
for course in sorted(unique_courses):
    print(f'- {course}')

print(f'\n总课程数: {len(unique_courses)}')